
(function () { 
	Lhs.ui.CreateTableWindow = function () {
	var pathimage=Ti.App.Properties.getString('pathimage');
	var platformWidth=Titanium.Platform.displayCaps.platformWidth;//Ti.App.Properties.getInt('platformWidth');
	var platformHeight=Titanium.Platform.displayCaps.platformHeight;//Ti.App.Properties.getInt('platformHeight');
    var win = Titanium.UI.createWindow({
        id: 'Table',
        title: 'TableAll',
        fullscreen: true,
        //backgroundImage: pathimage+'panel_right.png',
        navBarHidden:true,
        //exitOnClose: true,
        backgroundColor:'#e3e3e3',
        orientationModes:[
	        Titanium.UI.LANDSCAPE_LEFT,
	        Titanium.UI.LANDSCAPE_RIGHT,
		]
    });
  
    var egc = new globals.EventGarbageCollection(win);
    egc.addEventListener('back_'+win.id, function () {
    	Lhs.navGroup.close(win, {
             animated: true
        });
    	
	});
	
	var header = Ti.UI.createView({
	    top: 0,
	    height:30,
	    width:platformWidth,
	    backgroundColor: '#ffffe0',
	});
	win.add(header);
	var lb1 = Ti.UI.createLabel({
	  	text:'Chọn bàn mà bạn muốn đặt thực đơn',
	  	color:'black',
	  	font:{
				fontSize:17,//Ti.App.Properties.getInt('fontsize_header'),
				fontWeight:'bold',
		},
	});
	header.add(lb1);
	
	var LoadLocationControl = function(data) {
 		//////////////LOCATION///////////////
		var location=require('controls/location').Createlocation({
				top:header.height,
				idWinParent:win.id,
		 		width:Ti.App.Properties.getInt('TableWindow_LocationWidth'),
		 		height:platformHeight-header.height,
			    data:data
	    })
	    win.add(location)
    }
	
	
	
	//TABLE VIEW
    var tb_Table=Ti.UI.createTableView({
		  	backgroundColor:'Transparent',
		  	separatorColor:'Transparent',
		  	top:header.height,
		  	height:platformHeight-header.height,
		  	width:platformWidth-150,
		  	left:0,
		  	right:0,
		  	bottom:0,
    })
	tb_Table.addEventListener('click', function (e) {
    	
    	
	});
	win.add(tb_Table);
	
	
	
	
    //////////////LIST TABLE///////////////
    var row_in=Ti.App.Properties.getInt('Table_row_in');
    var col_in=Ti.App.Properties.getInt('Table_col_in');
    var LoadTablesDemo_Data = function(TableScreen_Id) {
 		var json=require('db').selectTableScreenTables(TableScreen_Id);
		return json;
    }
  
    var width_bt=tb_Table.width/col_in;
    var height_bt=tb_Table.height/row_in;
    var createButton = function (bt,left) {
		
		var backgroundImage,backgroundSelectedImage
		Ti.API.info('Ticket:'+bt.TicketId)
		if(bt.TicketId==0)
		{
			 backgroundImage=pathimage+'tb1.png'
			 backgroundSelectedImage=pathimage+'bt_Green.png'
		}
		else
		{
			Ti.API.info('IsTicketLocked:'+bt.IsTicketLocked)
			if(bt.IsTicketLocked==0)
			{
				 backgroundImage=pathimage+'bt_Green.png'
				 backgroundSelectedImage=pathimage+'bt_Green.png'
			}
			else
			{
				 backgroundImage=pathimage+'bt_red.png'
				 backgroundSelectedImage=pathimage+'bt_Green.png'
			}
		}
		var bta = Titanium.UI.createButton({
			/*
			 borderColor: '#c0c0c0',
		     borderRadius: 5,
		     borderWidth:4,
		     */
		     borderColor: Ti.App.Properties.getString('borderColor'),
		     borderRadius: Ti.App.Properties.getInt('borderRadius'),
		     borderWidth:Ti.App.Properties.getInt('borderWidth'),
			 color:'Black',
			 Table_Id:bt.Table_Id,
			 TableScreen_Id:bt.TableScreen_Id,
			 TicketId:bt.TicketId,
			 title:bt.Name,
			 backgroundImage:backgroundImage,
			 backgroundSelectedImage:backgroundSelectedImage,
			 width:width_bt,
		 	 height:height_bt,
		   	 left:left,
		   	 font:{
				fontSize:Ti.App.Properties.getInt('MainWindow_btnumber_fontsize'),
				fontWeight:'bold',
		 	 },
		   	 	 
		}); 
		bta.addEventListener('click', function (e) {
			//Ti.API.info ('TicketId:'+e.source.TicketId)
			////////////GET INFO////////////
			require('settings').ClickSound();
			//var json_Tickets=require('db').selectTickets(e.source.TicketId);
			//var json_Tickets=require('db').selectTables(e.source.Table_Id);
			
			var json_Tickets=require('db').selectTickets(e.source.TicketId);
			Lhs.navGroup.open(Lhs.ui.CreateOrderWindow({
					Name:e.source.title,
					TicketId:e.source.TicketId,
					json_Tickets:json_Tickets,
					TablesId:e.source.Table_Id,
					TableName:e.source.title,
			}), 
			{
			    animated: true,
			    navBarHidden:true
			});	
	   })
	   return bta;
	}
	
    var CreateTableViewRow = function(e) {
		
		var item=e.data;
		
		
		var row = Ti.UI.createTableViewRow({
	            height:tb_Table.height/row_in,
				
		})
	
		var left_bt=0;
		for (var c=0;c<item.length;c++){
			
			if(item[c]!=undefined)
			{
				var bt=createButton(item[c],left_bt)
				row.add(bt);
				left_bt+=width_bt;
			}
			
		}
		return row;
	};
    var LoadTables = function(TableScreen_Id) {
    	
 		var tableData=[];
 		var json=[];
 		
 		if (Ti.App.Properties.getInt('IsDemo')==1) 
 		{
 			json=LoadTablesDemo_Data(TableScreen_Id);
 		}
 		else
 		{
 			
 		}
 		
 		
 		require('settings').LoadedData2TableView(tb_Table,json,CreateTableViewRow,col_in);
	};
	//////////////LOCATION///////////////
	var LoadLocationDemo_Data = function() {
 		var json=require('db').selectTableScreens();
		return json;
    }
    var LoadLocation = function(e) {
    	
 		var tableData=[];
 		var json=[];
 		
 		if (Ti.App.Properties.getInt('IsDemo')==1) 
 		{
 			json=LoadLocationDemo_Data();
 		}
 		else
 		{
 			
 		}
 		
 		
 		LoadLocationControl(json);
	};
	
	

	
	var LocationId=1
	egc.addEventListener('TableWindow:locationSelect', function (e) {
		LocationId=e.data
		LoadTables(e.data)
	})
	egc.addEventListener('TableWindow:LoadTables', function (e) {
		
		LoadTables(LocationId)
	})
	/////////////EVEN/////////////////////////
	win.addEventListener('open', function (e) {
		LoadLocation(e)
		LoadTables(1)
	});
	
	
	// removing the event listener does
	win.addEventListener('close', function() {
		//Ti.App.removeEventListener('TableWindow:locationSelect',LoadTables);
	});
	
	
 	return win;
       
    };
})();